#include <bits/stdc++.h>
using namespace std;
int x,y;
int k = 0;

int gcd(int x,int y)
{
	if (x == y)
		return x;
	if (x < y)
		gcd(x,y-x);
	if (x > y)
		gcd(x-y,y);
}
			


int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		cin >> x >> y;
		int m = gcd(x,y);
		y = y / m;
		while(y % 6 == 0)
		{
			y /= 6;
			k++;
		}
		while(y % 5 == 0)
		{
			y /= 5;
			k++;
		}
		if(y != 1)
		{
			cout << -1 << endl;
		}
		k = 0;
	}
	return 0;
 } 
